package com.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.model.Staff;
import com.service.IStaffService;
import com.utils.MeiJiaUtils;

@Controller
@RequestMapping("/staff")
public class StaffController {
	@Resource
	private IStaffService staffService;
	//@Resource
	//private ILoggingService loggingService;
	private int page = 10;//每页分10条	
	
	/**
	 * 判定老板和员工输入的用户名和密码是否正确
	 * @param username
	 * @param password
	 * @param request
	 * @return
	 */
	@RequestMapping("/login")
	public String login(String username, String password, HttpServletRequest request){
		String error = "";
		if(MeiJiaUtils.isEmpty(username) || MeiJiaUtils.isEmpty(password)){
			error = "用户名或密码不能为空！";
			request.setAttribute("error", error);
			return "login";
		}
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("username", username);
		map.put("password", password);
		Staff staff = staffService.selectByUsernameAndPassword(map);
		if(staff == null){
			error = "用户名或密码错误！";
			request.setAttribute("error", error);
			request.setAttribute("username", username);
			return "login";
		}
		if(staff.getStatus().equals(0)){
			error = "该账号已经失效！";
			request.setAttribute("error", error);
			return "login";
		}
		
		staff.setLasttime(new Date());
		staffService.updateStaff(staff);
		
		request.getSession().setAttribute("sessionStaff", staff);

		request.setAttribute("staff", staff);
		return "main";
	}
	
	/**
	 * 退出系统
	 * @param request
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request){
		request.getSession().removeAttribute("sessionStaff");
		return "login";
	}
	
	
	@RequestMapping("/showStaff")
	public String showStaff(Staff s, String pages,HttpServletRequest request) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("name", s.getName());
		map.put("username", s.getUsername());
		map.put("status", s.getStatus());
		map.put("telephone", s.getTelephone());
		
		int curPage = 0;
		if (pages == null || pages.trim().equals("")) {
			curPage = 1;
		} else {
			curPage = Integer.parseInt(pages);
		}
		request.setAttribute("curPage", curPage);
		map.put("min", (curPage - 1) * page);
		map.put("max", page);
		List<Staff> u = staffService.findAll(map);
		int count =staffService.countStaffs();
		if (count % page == 0) {
			request.setAttribute("pageNumber", count / page);
		} else {
			request.setAttribute("pageNumber", count / page + 1);
		}
		request.setAttribute("count", count);
		request.setAttribute("staffList", u);
		request.setAttribute("s", s);
		return "showStaff";
	}
	
	@RequestMapping("/saveStaff")
	public String saveStaff(Staff u,HttpServletRequest request){
		u.setStatus(1); //在职
		u.setIsAdmin(0);//非老板
		u.setCreatetime(new Date());
		u.setLasttime(new Date());
		u.setPassword("123456");
		staffService.saveStaff(u);
		return "redirect:/staff/showStaff.action";
	}
	
	@RequestMapping("/saveStaffForm")
	public String saveStaffForm(){
		return "saveStaff";
	}
	
	/**
	 * 输入.../meijia进入到登录页面
	 * @return
	 */
	@RequestMapping("/index")
	public String indexForm(){
		return "login";
	}
	
	/**
	 * 修改密码页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/updatePasswordForm")
	public String updatePasswordForm(HttpServletRequest request){
		Staff staff = (Staff)request.getSession().getAttribute("sessionStaff");
		Staff s = staffService.getStaffById(staff.getId());
		request.setAttribute("staff", s);
		return "updatePassword";
	}
	/**
	 * 修改密码
	 * @param request
	 * @return
	 */
	@RequestMapping("/updatePassword")
	public String updatePassword(Staff s, HttpServletRequest request){
		staffService.updateStaff(s);
		return "redirect:/staff/showStaff.action";
	}
	
	
	@RequestMapping("/updateStaffForm")
	public String updateStaffForm(int id,HttpServletRequest request){
		Staff u = staffService.getStaffById(id);
		request.setAttribute("staff", u);
		return "updateStaff";
	}
	
	@RequestMapping("/updateStaff")
	public String updateStaff(Staff u,HttpServletRequest request){
		staffService.updateStaff(u);
		return "redirect:/staff/showStaff.action";
	}
	

	@RequestMapping("/cancelStaff")
	public String cancelStaff(Integer id,HttpServletRequest request){
		Staff u = staffService.getStaffById(id);
		u.setStatus(0); //离职
		staffService.updateStaff(u);
		return "redirect:/staff/showStaff.action";
	}
	
}
